package com.zmn.plat.admin.controller.orderconfig.orderstock;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Resource;

import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.dto.VtDTO;
import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.manager.interfaces.cache.ServCategoryCache;
import com.zmn.plat.model.entity.servcategory.ServCategory;

public class OrderStockBaseController extends PlatBaseController {

    @Resource
    ServCategoryCache servCategoryCache;

    /**
     * 描述: 日期列表
     *
     * @param mav
     *            查询实体
     * @return ModelAndView
     */
    public ModelAndView dutyDate(ModelAndView mav) {
        LocalDate start = LocalDate.now();
        LocalDate end = start.plusWeeks(4);
        List<LocalDate> dates = Stream.iterate(start, date -> date.plusDays(1)).limit(ChronoUnit.DAYS.between(start, end))
            .collect(Collectors.toCollection(LinkedList::new));
        // 获取当天周数
        int weekValue = start.getDayOfWeek().getValue();
        for (int i = 1; i < weekValue; i++) {
            dates.add(0, null);
        }
        int totalSize = dates.size();
        int pageSize = 7;
        int maxPage = totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize) + 1;

        List<List<LocalDate>> items = new ArrayList<>();
        for (int page = 1; page <= maxPage; page++) {
            int startIndex = (page - 1) * pageSize;
            int endIndex = page == maxPage ? totalSize : (page * pageSize);
            List<LocalDate> row = dates.subList(startIndex, endIndex);
            items.add(row);
        }
        mav.addObject("items", items);
        return mav;
    }

    public List<VtDTO> listShowServCategory() {
        List<ServCategory> servCategoryDROList = servCategoryCache.listAll();
        List<VtDTO> vts =
            servCategoryDROList.stream().map(item -> new VtDTO(item.getCategId(), item.getShowName())).collect(Collectors.toList());
        return vts;
    }

    public List<VtDTO> listServCategory() {
        List<ServCategory> servCategoryDROList = servCategoryCache.listAll();
        List<VtDTO> vts =
            servCategoryDROList.stream().map(item -> new VtDTO(item.getCategId(), item.getName())).collect(Collectors.toList());
        return vts;
    }
}
